﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Text;
using System.Text.RegularExpressions;

using System.Net;
using System.IO;
/// <summary>
/// Sql 的摘要说明

/// </summary>
public class Helper
{
    public Helper()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    public static string strRandom = "";
    static string strRandomOld = "";
    public static string CreateRandomStr()
    {

        //*********************************************************
        //随机数
        //*********************************************************
        Random r = new Random();
        while (strRandomOld == strRandom)
        {
            strRandom = r.Next(100000).ToString();
        }

        strRandomOld = strRandom;
        return strRandom;

    }

    public static string GetResult(string strUrl)
    {
        try
        {
           


            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(strUrl);
            
            CookieContainer CCt = new CookieContainer();
            myHttpWebRequest.CookieContainer = CCt;



         
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
            Stream receiveStream = myHttpWebResponse.GetResponseStream();
            Encoding encode = System.Text.Encoding.UTF8;
            StreamReader readStream = new StreamReader(receiveStream, encode);

            Char[] read = new Char[256];
            int count = readStream.Read(read, 0, 256);
            StringBuilder sb = new StringBuilder();

            while (count > 0)
            {

                string str = new String(read, 0, count);
                sb.Append(str);
                count = readStream.Read(read, 0, 256);
            }

            readStream.Close();

            return sb.ToString();


        }catch(Exception ee)
        {

            return "无法找到地址：" + strUrl + "";
        }
    }


    public static string ChangeCharset(string Input,string charsetFrom,string charsetTo)
    {
        System.Text.Encoding gb = System.Text.Encoding.GetEncoding(charsetFrom);
        byte[] b = gb.GetBytes(Input);
        System.Text.Encoding gb1 = System.Text.Encoding.GetEncoding(charsetTo);
        string g = gb1.GetString(b);
        return g;


    }

    public static string ChangeCharsetGbtoU8(string Input)
    {
        return ChangeCharset(Input, "gb2312", "utf-8");
    
    }


    /// <summary>
    /// 转码
    /// </summary>
    /// <param name="Input"></param>
    /// <returns></returns>
    public static string ChangeCharset(string Input)
    {
        //System.Text.Encoding gb = System.Text.Encoding.Default;
        System.Text.Encoding gb = System.Text.Encoding.GetEncoding("utf-8");
        //System.Text.Encoding gb = System.Text.Encoding.GetEncoding("ISO-8859-1");

        byte[] b = gb.GetBytes(Input);

        System.Text.Encoding gb1 = System.Text.Encoding.GetEncoding("ISO-8859-1");
        //System.Text.Encoding gb1 = System.Text.Encoding.GetEncoding("latin1");
        //System.Text.Encoding gb1 = System.Text.Encoding.GetEncoding("gb2312");
        //System.Text.Encoding gb1 = System.Text.Encoding.GetEncoding("latin1");
        string g = gb1.GetString(b);

        return g;



    }

    public static DataTable ChangeColumn(DataTable dt, int id, string value)
    {
        if (dt != null)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i][id] = value;
            }
        }
        return dt;
    }

    public static DataTable DealTable_Str(DataTable gDataTable)
    {
        DataTable xReturn = gDataTable;
        if (xReturn == null)
        {
            return xReturn;
        }
        if (xReturn.Rows.Count == 0)
        {
            return xReturn;
        }
        try
        {
            string[] xName = GetStringField(xReturn);
            if (xName != null && xName.Length > 0)
            {
                for (int i = 0; i < xReturn.Rows.Count; i++)
                {
                    for (int j = 0; j < xName.Length; j++)
                    {

                        if (xReturn.Rows[i][xName[j]] != null && !(Convert.IsDBNull(xReturn.Rows[i][xName[j]])))
                        {
                            string y = (string)(xReturn.Rows[i][xName[j]]);
                            if (y != "")
                            {
                                //byte[] buf = System.Text.Encoding.GetEncoding(28591).GetBytes(y);
                                byte[] buf = System.Text.Encoding.GetEncoding("latin1").GetBytes(y);
                                y = System.Text.Encoding.GetEncoding("GB2312").GetString(buf);
                                xReturn.Rows[i][xName[j]] = y;
                            }
                        }
                    }
                }
            }

        }
        catch
        {
        }

        return xReturn;
    }
    static String[] GetStringField(DataTable gDataTable)
    {
        string[] xReturn = null;
        DataTable xDT = gDataTable;
        if (xDT == null)
        {
            return xReturn;
        }
        if (xDT.Rows.Count == 0)
        {
            return xReturn;
        }
        try
        {
            string xCol = "";
            for (int i = 0; i < xDT.Columns.Count; i++)
            {

                Type xType = xDT.Columns[i].DataType;
                if (xType == typeof(string))
                {
                    //byte[] buf = System.Text.Encoding.GetEncoding(28591).GetBytes(xDT.Columns[i].ColumnName);
                    //xDT.Columns[i].ColumnName = System.Text.Encoding.Default.GetString(buf);
                    xCol += "|" + xDT.Columns[i].ColumnName;
                }
            }
            if (xCol.Length > 0)
            {
                xCol = xCol.Remove(0, 1);
            }
            xReturn = xCol.Split('|');
        }
        catch
        {
            xReturn = null;
        }
        return xReturn;
    }

    public static string HtmlCoding(string strIn)
    {
        strIn = strIn.Replace(" ", "&nbsp;");
        strIn = strIn.Replace("\n", "<br>");

        return strIn;
    }

    static string CapText(Match m)
    {
        // Get the matched string.
        //sstring x = m.ToString();
        // If the first char is lower case...
        //if (char.IsLower(x[0]))
        //{
        //    // Capitalize it.
        //    return char.ToUpper(x[0]) + x.Substring(1, x.Length - 1);
        //}

        return "";
    }

    public static string DeleteEnter(string strIn)
    {
        strIn = strIn.Replace("\n", "");
        strIn = strIn.Replace("\r", "");
        return strIn;
    }

    public static string DeHtmlCoding(string strIn)
    {
        strIn = strIn.Replace("&nbsp;", " ");
        //strIn = strIn.Replace("<br>", "\n");

        Regex r = new Regex("<.*?>");
        strIn = r.Replace(strIn, new MatchEvaluator(CapText));
        //Match m = r.Match(strIp);

        return strIn;
    }

    public static string DeHtmlCodingStyle(string strIn)
    {
        strIn = strIn.Replace("&nbsp;", " ");

        Regex r = new Regex("<STYLE>.*?</STYLE>?");
        strIn = r.Replace(strIn, new MatchEvaluator(CapText));

        //strIn = strIn.Replace("<span", "!span");
        //strIn = strIn.Replace("span>", "!span");
        //strIn = strIn.Replace("<span".ToUpper(), "!span");
        //strIn = strIn.Replace("span>".ToUpper(), "!span");


        r = new Regex("<.*?>");
        strIn = r.Replace(strIn, new MatchEvaluator(CapText));


        return strIn;
    }


    public static string GetDateString(string strIn)
    {
        if (strIn == "")
        {
            return "";
        }

        DateTime d1 = Convert.ToDateTime(strIn);
        strIn = d1.ToString("yy-MM-dd");
        return strIn;
    }

    public static string DeHtmlCodingBlank(string strIn)
    {
        strIn = strIn.Replace("&nbsp;", " ");


        return strIn;
    }

    public static string StringAddStyle(string strChange)
    {
        StringBuilder strIn = new StringBuilder();
        strIn.Append(strChange);

        strIn.Append("<style>\n");
        strIn.Append(" BODY, TD, INPUT, TEXTAREA, SELECT \n");
        strIn.Append("   {FONT-FAMILY: Arial,Verdana; FONT-SIZE: 9pt; COLOR: black; }\n");
        strIn.Append(" BODY {margin:4px; BACKGROUND-COLOR:#eaeef7;}\n");
        strIn.Append(" TABLE {EMPTY-CELLS: show;  BORDER-COLLAPSE:collapse;}\n");
        strIn.Append("\n");
        strIn.Append("TD {padding-right:2px; FONT-FAMILY: Monospace}\n");
        strIn.Append(".i {COLOR:black; BACKGROUND-COLOR:#80FF80}\n");
        strIn.Append(".d {COLOR:black; BACKGROUND-COLOR:#FF8080}\n");
        strIn.Append(@"p{style=""border-bottom: 1px solid #C1DAD7;height:15px;margin:0;""}");
        strIn.Append(@".rq{color:blue;pading-left:3px;float:right;}");
        strIn.Append(@".bz{color:#333;float:right;}");
        strIn.Append("    </style>\n");



        return strIn.ToString();
    }

    public static string GetStyleString()
    {
        StringBuilder strIn = new StringBuilder();
        strIn.Append("<style>\n");
        strIn.Append(" BODY, TD, INPUT, TEXTAREA, SELECT \n");
        strIn.Append("   {FONT-FAMILY: Arial,Verdana; FONT-SIZE: 9pt; COLOR: black; }\n");
        strIn.Append(" BODY {margin:4px; BACKGROUND-COLOR:#eaeef7;}\n");
        strIn.Append(" TABLE {EMPTY-CELLS: show;  BORDER-COLLAPSE:collapse;}\n");
        strIn.Append("\n");
        strIn.Append("TD {padding-right:2px; FONT-FAMILY: Monospace}\n");
        strIn.Append(".i {COLOR:black; BACKGROUND-COLOR:#80FF80}\n");
        strIn.Append(".d {COLOR:black; BACKGROUND-COLOR:#FF8080}\n");
        strIn.Append("    </style>\n");



        return strIn.ToString();
    }

}
